package post

import (
	"github.com/gin-gonic/gin"
	"apiserver/handler"
	"apiserver/errno"
	"apiserver/service"
)

func List(c *gin.Context) {
	var r ListRequest
	if err := c.Bind(&r); err != nil {
		handler.SendResponse(c, errno.ErrBind, nil)
		return
	}
	if r.Page <= 0 {
		handler.SendResponse(c, errno.ErrPostPage, nil)
		return
	}
	lists, count, err := service.ListPost(r.UserId, r.Page, r.Limit)
	if err != nil {
		handler.SendResponse(c, err, nil)
		return
	}
	var nextPage int
	if len(lists) == 0 {
		nextPage = 0
	} else {
		nextPage = r.Page + 1
	}
	handler.SendResponse(c, nil, ListResponse{
		Total:    count,
		PostList: lists,
		NextPage: nextPage,
	})
}
